package com.imooc.luckymoney.resource;

import com.imooc.luckymoney.repository.GroupCollection;
import com.imooc.luckymoney.service.ExcelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(value = "test", tags = {"Excel相关接口"})
@RequestMapping("/api")
public class ExcelResource {

    @Autowired
    private ExcelService excelService;

    @PostMapping("/save")
    @ApiOperation("添加接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", defaultValue = "李四"),
            @ApiImplicitParam(name = "address", value = "用户地址", defaultValue = "深圳", required = true)
    }
    )
    public String saveOne(@RequestBody GroupCollection groupCollection) {
        excelService.save(groupCollection);

        return "ok";
    }

    @PutMapping("/updateOne")
    public String updateOne(@RequestBody GroupCollection groupCollection) {
        excelService.updateOne(groupCollection);

        return "ok";
    }

    @PostMapping("/upsert")
    public String upsert(@RequestBody GroupCollection collection) throws IllegalAccessException {
        excelService.createOrUpdateById(collection);

        return "ok";
    }

    @GetMapping("/test1")
    public List<GroupCollection> findByIds() {
        List<GroupCollection> collections = excelService.findByIds(new ArrayList<String>());

        return collections;
    }

    @PutMapping("/updateOneObj")
    public String updateOneObj(@RequestBody GroupCollection collection) {
        long updateOneObj = excelService.updateOneObj(collection);
        return String.valueOf(updateOneObj);
    }

    @DeleteMapping("/{id}")
    public long deleteById(@PathVariable String id) {
        long res = excelService.removeById(id);
        return res;
    }

    @GetMapping("/test")
    public String test() {
        return "ok";
    }
}
